<template>
  <el-container class="home-container">
    <!--头部布局-->
    <el-header>
      <div>
        <!-- <img src="../assets/logo.jpg" alt class="logo_img" /> -->
        <span>考试管理系统</span>
      </div>
      <el-button type="info" @click="logout">安全退出</el-button>
    </el-header>
    <el-container>
      <!--侧边布局-->
      <el-aside :width="isCollapse ?'64px':'200px'">
        <!--伸缩按钮-->
        <div class="toggle-button" @click="toggleCollapase">展开</div>
        <!--侧边栏菜单区 unique-opened="true" 只保持一个菜单展开 router开启路由 active-text-color 颜色-->
        <el-menu background-color="#545c64" text-color="#fff" active-text-color="#409eff" unique-opened :collapse="isCollapse" 
          :collapse-transition="false"  :router="true" :default-active="activePath">
          <el-submenu :index="item.id+''" v-for="item in menuList" :key="item.id">
            <template slot="title">
              <i :class="iconsObject[item.id]"></i>
              <span>{{item.title}}</span>
            </template>
            <el-menu-item  :index="it.path+''" v-for="it in item.slist" :key="it.id"  @click="saveNavState(it.path+'')">
              <template slot="title" >
                <i :class="iconsObject[it.id]"></i>
                <span>{{it.title}}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!--主体布局-->
      <el-main>
          <!--路由占位符-->
          <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>
<script>
export default {
//   为什么要写return返回
// 因为不使用return包裹的数据会在项目的全局可见，会造成变量污染
// 使用return包裹后数据中变量只在当前组件中生效，不会影响其他组件。
    data() {
        return {
            // 左侧菜单
            menuList: [

    {
            "id": 100,
            "path": "/admin",
            "slist": [
                {
                    "id": 101,
                    "path": "/user",
                    "title": "学生列表"
                },
                {
                    "id": 102,
                    "path": "/teacher",
                    "title": "老师列表"
                },
                {
                    "id": 103,
                    "path": "/file",
                    "title": "文件相关"
                },
              
            
            ],
            "title": "用户管理"
        },
        {
            "id": 200,
            "path": "/exam",
            "slist": [
                {
                    "id": 201,
                    "path": "/examList",
                    "title": "试卷列表"
                },
                {
                    "id": 202,
                    "path": "/createExam",
                    "title": "试卷创编"
                },
              {
                "id": 203,
                "path": "/publishExam",
                "title": "试卷发布"
              },
            
            ],
            "title": "试卷管理"
        },
     {
            "id": 300,
            "path": "/use",
            "slist": [
                {
                    "id": 201,
                    "path": "/problemmList",
                    "title": "题目列表"
                },
                {
                    "id": 202,
                    "path": "/singleProblem",
                    "title": "单选创编"
                },
                 {
                    "id": 202,
                    "path": "/multipleProblem",
                    "title": "多选创编"
                },
                {
                    "id": 203,
                    "path": "/essayProblem",
                    "title": "问答创编"
                },
                {
                    "id": 204,
                    "path": "/judgmentProblem",
                    "title": "判断创编"
                },
              
            ],
            "title": "题库管理"
        },
             {
            "id": 400,
            "path": "/use",
            "slist": [
                {
                    "id": 201,
                    "path": "/correctList",
                    "title": "批改列表"
                },
                {
                    "id": 202,
                    "path": "/readed",
                    "title": "已阅试卷"
                },
             
            ],
            "title": "答卷管理"
        },

            ],
            iconsObject: {
              '100':'iconfont iconguanliyuan',
              '200':'iconfont iconsport',
              '300':'iconfont iconguanliyuan',
              '400':'iconfont iconsport',
              '101':'iconfont icondenglu',
              '102':'iconfont iconmima',
              '103':'iconfont iconsport',
              '104':'iconfont iconshangpin',
              '201':'iconfont iconshu',
              '202':'iconfont iconkaluli',
              '203':'iconfont iconshiwu',
              '204':'iconfont icondenglu',
            },
            isCollapse:false,
            // 被激活的连接
            activePath:'',
        }
    },
  // 类似onload  一进来就查询
  created() {
      this.getMenuList();
      this.activePath = window.sessionStorage.getItem('activePath');// 取出session里的访问路径
  },
  methods: {
    logout() {
      window.sessionStorage.clear();  //退出的时候，就把登录时的数据给清除了
      this.$router.push("/login");
    },
    // 获取所有的导航菜单
    async getMenuList(){
      const {data:res} = await this.$http.get("/menus1");  //执行操作的统一写法
      console.log(res.data);
      if( res.status != 200) ;
    // return this.$message.error("操作失败！！！");
    // this.menuList = res.data;
    },
    // 切换菜单折叠与展开
    toggleCollapase(){
      this.isCollapse = !this.isCollapse;
    },
    // 保存二级菜单的路径
    saveNavState(activePath){
      window.sessionStorage.setItem('activePath',activePath);// 存放点击的二级菜单
      this.activePath = activePath;// 给点击的菜单添加高亮
    },
  }
};
</script>
<style lang="less" scoped>
.el-header {
  background-color: #373d41;
  display: flex;
  justify-content: space-between;// 左右贴边
  padding-left: 0%;// 左边界
  align-items: center;// 水平
  color: #fff;
  font-size: 20px;
  > div { //左侧div加布局
    display: flex;
    align-items: center;
    span {
      margin-left: 15px;
    }
  }
}
.el-aside {
  background-color: #333744;
  .el-menu{
    border-right: none;// 对其右边框
  }
}
.el-main {
  background-color: #eaedf1;
}
.home-container {
  height: 100%;
}
.logo_img {
  width: 20%;
  height: 100%;
}
.iconfont{
  margin-right: 10px;
}
.toggle-button{
  background-color:#4A5064;
  font-size: 10px;
  line-height: 24px;
  color:#fff;
  text-align: center;
  letter-spacing: 0.2em;
  cursor: pointer;// 显示鼠标指针为：小手
}
</style>